package com.lml.jdk8.test05;

import org.junit.jupiter.api.Test;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @date 2020/3/2 13:26
 * Description:
 */
public class DemoTest02 {

    static boolean flag = false;

    @Test
    void fun6() {

    }

    @Test
    void fun5() {
        new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {

            }
            flag = true;
        }).start();

        while (true) {
            if (flag) {
                System.out.println("flag changed");
            } else {
                System.out.println("flag not changed");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    void fun4() {
        LocalDateTime begin = LocalDateTime.of(2019, 12, 15, 23, 0, 0);
        LocalDateTime now = LocalDateTime.now();
        Duration between = Duration.between(begin, now);
        System.out.println("相差天数:" + between.toDays());
    }

    @Test
    void fun3() {
        //计算两个时间相差多久
        LocalTime beginTime = LocalTime.of(20, 0, 0);
        LocalTime endTime = LocalTime.of(20, 10, 0);

        Duration between = Duration.between(beginTime, endTime);
        System.out.println(between.getSeconds());

        //计算两个日期相差多少天
        LocalDate beginDate = LocalDate.parse("2020-01-31");
        LocalDate endDate = LocalDate.parse("2020-02-10");
        //注意:日期小的放在前面
        Period p = Period.between(beginDate, endDate);
        System.out.println(p.getYears());
        System.out.println(p.getMonths());
        System.out.println(p.getDays());

    }

    @Test
    void dateStudy() {
        LocalDate today = LocalDate.now();
        System.out.println(today);

        int year = today.getYear();
        int monthValue = today.getMonthValue();
        int dayOfMonth = today.getDayOfMonth();

        LocalDate date1 = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("一周后的日期为:" + date1);

        LocalDate localDate = LocalDate.of(2020, 1, 1);
        LocalDate parse = LocalDate.parse("2020-01-01");
        System.out.println(parse);

    }

    @Test
    void fun1() {
        String[] strings = new String[]{"1", "2", "3"};
        Stream<String> stream = Arrays.stream(strings);
        int sum = stream.mapToInt(Integer::valueOf).sum();
        System.out.println(sum);
    }
}
